/*
 * @Author: zs.duan
 * @Date: 2023-05-04 19:39:27
 * @LastEditors: zs.duan
 * @LastEditTime: 2023-05-04 19:43:01
 * @FilePath: \serve\routes\uploadFile.js
 */
const express = require("express");
const router = express.Router();
const { uploadFile, uploads } = require("../utils/cbFile");
const { errorCode, successCode, config } = require('../config/config');
const multer = require('multer');
const formdata = multer();
const path = require('path');
const fs = require('fs');

/**
 * @swagger
 * /api/upload:
 *    post:
 *      tags:
 *      - 文件管理
 *      summary: 上传文件
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                file:
 *                  type: file
 *                  description: 文件
 *                  required: true
 *      responses:
 *        200:
 *          description: 返回结果
 * */

router.post("/", formdata.any(), async (req, res) => {
    uploads(req.files, {
        FilePath: req.body.FilePath || "../upload/file",
        fileName: req.body.fileName,
        success: (files) => {
            if (files.data.length == 1) {
                return res.json({
                    ...successCode[200],
                    data: {
                        ...files.data[0],
                        path: req.body.FilePath ? req.body.FilePath : config.httpFileUrl,
                        baseUrl: config.httpImgUrl
                    },
                })
            }
            return res.json({
                ...successCode[200],
                data: {
                    data: files.data,
                    path: config.httpFileUrl
                }
            })
        },
        fail: (err) => {
            console.log(err)
            res.status(500);
            return res.json({
                ...errorCode[10009],
            })
        }
    })
})

/**
 * @swagger
 * /api/upload/uploadImg:
 *    post:
 *      tags:
 *      - 文件管理
 *      summary: 上传图片
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                file:
 *                  type: file
 *                  description: 文件
 *                  required: true
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/uploadImg", async (req, res) => {
    uploadFile(
        req,
        {
            FilePath: "../upload/img"
        },
        (file) => {
            if (file.code != 200) {
                res.status(500);
                return res.json(file)
            }
            res.json({
                ...successCode[200],
                data: {
                    ...file.data,
                    path: config.httpImgUrl,
                    url: 'img/' + file.data.fileName
                },
            })
        }
    )
})

/**
 * @swagger
 * /api/upload/editor:
 *    post:
 *      tags:
 *      - 文件管理
 *      summary: 富文本编辑器上传图片
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                file:
 *                  type: file
 *                  description: 文件
 *                  required: true
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/editor", async (req, res) => {
    uploadFile(
        req,
        {
            FilePath: "../upload/editor"
        },
        (file) => {
            if (file.code != 200) {
                res.status(500);
                return res.json(file)
            }
            res.json({
                ...successCode[200],
                data: {
                    url: "editor/" + file.data.fileName,
                    path: config.httpImgUrl
                }
            })
        }
    )
})

/**
 * @swagger
 * /api/upload/wxIcon:
 *    post:
 *      tags:
 *      - 文件管理
 *      summary: 微信小程序上传图片
 *      produces:
 *      - application/json
 *      requestBody:
 *        description: Optional description in *Markdown*
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                file:
 *                  type: file
 *                  description: 文件
 *                  required: true
 *      responses:
 *        200:
 *          description: 返回结果
 * */
router.post("/wxIcon", async (req, res) => {
    uploadFile(
        req,
        {
            FilePath: "../upload/wxIcon"
        },
        (file) => {
            if (file.code != 200) {
                res.status(500);
                return res.json(file)
            }
            res.json({
                ...successCode[200],
                url: "wxIcon/" + file.data.fileName,
                path: config.httpImgUrl
            })
        }
    )
})


module.exports = router;